window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		var loadfail = "../images/icons/loadfail.png";
		var curPage = 1;
		var pageSize = 5;
		var totalPage = 0;
		var result = new Object();
		U.fillHead();
		U.fillFooter();
		initPage();

		function initPage() { //初始化页面
			U.initScrollImg('bar', null, 2000);
			U.loadOpt(false);
			getlist();
		}

		function initEvent() {
			$(".policy_list>div").click(function() {
				var news = result[$(this).index()];
				console.info(news);
				if(news.contentType == 1) {
					window.open(news.content);
				} else {
					window.open("Policy_details.html?id=" + $(this).attr("id"));
				}
			});
		}
		//分页方法
		function paging_handle(page, method) {


			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;

			if(lastsize>=totalPage){
				lastsize=totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!")
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕


		}

		function getlist() {
			var params = {
				curPage: curPage,
				pageSize: pageSize,
				newsTypeId: 0
			};
			U.loadOpt(true);
			var id = U.getParam("id");
			if(id == null) {
				id = 0;
			}
			if(U.getParam("onlynews") == "1") {
				params["contentType"] = 2;
			} else {
				params["contentType"] = 0;
			}
			U.loadOpt(true);
			U.ajaxAsync("news/queryByType?newsTypeId=" + id + "&curPage=" + curPage + "&pageSize=5&contentType=" + params["contentType"], {}, function(json) {
				U.loadOpt(false);
				$(".policy_list").empty();
				if(json.success) {
					totalPage = json.models.page.totalPage;
					result = json.models.newsDeList;
					$(json.models.newsDeList).each(function() {
						var img = loadfail;
						$(this.images.split(",")).each(function() {
							if(this == "") {
								return true;
							}
							img = this;
							return false;
						});
						var policy = "<div id='" + this.id + "'><div class='zx_img'><img src='" + img + "'></div><div class='zx_content'><div class='zx_title'>" + this.title + "</div><div class='zx_title_f'>" + this.subtitle + "</div><div class='continue'>继续阅读 &gt;</div></div><div class='zx_time'>" + U.getMyDate(this.gmtCreate) + "</div></div>";
						$(".policy_list").append(policy);
					});
					paging_handle(json.models.page, getlist);

					initEvent();
				} else {
					M.toast(false, json.errorMsg);
				}

			});
		}
	});
}